const path = require('path')
const buble = require('@rollup/plugin-buble')
const replace = require('@rollup/plugin-replace')
const version = process.env.VERSION || require('../package.json').version
const commonjs = require('@rollup/plugin-commonjs')
const nodeResolve = require('@rollup/plugin-node-resolve').default
const json = require('@rollup/plugin-json')
const babel = require('@rollup/plugin-babel').default
const banner =
  '/*!\n' +
  ` * WeChatAuth v${version}\n` +
  ` * (c) 2021-${new Date().getFullYear()} ChinaZhanghai(916979094@qq.com)\n` +
  ' * Released under the MIT License.\n' +
  ' */'
const resolve = p => {
  return path.resolve(__dirname, '../', p)
}
const builds = {
  'web-cjs-dev': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.common.dev.js'),
    format: 'cjs',
    env: 'development',
    banner
  },
  'web-cjs-prod': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.common.prod.js'),
    format: 'cjs',
    env: 'production',
    banner
  },
  'web-cjs': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.common.js'),
    format: 'cjs',
    env: 'production',
    banner
  },
  'web-esm': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.esm.js'),
    format: 'es',
    env: 'production',
    banner
  },
  'web-esm-browser-dev': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.esm.browser.js'),
    format: 'es',
    env: 'development',
    transpile: false,
    banner
  },
  'web-esm-broswer-prod': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.esm.browser.min.js'),
    format: 'es',
    env: 'production',
    transpile: false,
    banner
  },
  'web-dev': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.js'),
    format: 'umd',
    env: 'development',
    banner
  },
  'web-prod': {
    entry: resolve('src/index.ts'),
    dest: resolve('dist/auth.min.js'),
    format: 'umd',
    env: 'production',
    banner
  }
}

function genConfig (name) {
  const opts = builds[name]
  const extensions = ['.js', '.ts']
  const config = {
    input: opts.entry,
    external: ['qs', 'axios', 'js-cookie', 'qrcode'].concat(opts.external || []),
    output: {
      file: opts.dest,
      format: opts.format,
      banner: opts.banner,
      name: opts.moduleName || 'YinduOgilvy',
      globals: {
        qs: 'qs',
        'js-cookie': 'Cookies',
        axios: 'axios',
        qrcode: 'QRCode'
      }
    },
    plugins: [
      nodeResolve({
        extensions
      }),
      json(),
      commonjs(),
      // typescript({
      //   exclude: "node_modules/**",
      //   typescript: require('typescript'),
      //   sourceMap: false
      // }),
      babel({
        babelHelpers: "runtime",
        exclude: 'node_modules/**',
        extensions
      })
    ].concat(opts.plugins || [])
  }
  const vars = {
    preventAssignment: true,
    __VERSION__: version
  }
  if (opts.env) {
    vars['process.env.NODE_ENV'] = JSON.stringify(opts.env)
  }
  config.plugins.push(replace(vars))
  if (opts.transpile !== false) {
    config.plugins.push(buble())
  }
  Object.defineProperty(config, '_name', {
    enumerable: false,
    value: name
  })
  return config
}

if (process.env.TARGET) {
  module.exports = genConfig(process.env.TARGET)
} else {
  exports.getBuild = genConfig
  exports.getAllBuilds = () => {
    return Object.keys(builds).map(genConfig)
  }
}